/**
 * Created by chenzheyu on 2017/4/5.
 */
$(function () {
    var CompanyId='';
    var SEOval='';
    var limit=20;
    $.ajax({
        url:ctx+'/company/listData',
        type: 'GET',
        success: function (data) {
            // console.log(data)
            var DownPullSelectOption = {
                data: [],
                onChange: function (e) {
                    CompanyId = e.val;
                }
            };
            for (var i = 0; i < data.length; i++) {
                (function () {
                    var index = i;
                    DownPullSelectOption.data.push({
                        name: data[index].abbreviation,
                        Val: data[index].id
                    });
                })()
            }

            $('#SearchTerms').DownPullSelect(DownPullSelectOption);
            $('.CustomDownPullSelect').find('select').prepend('<option value="">全部</option>');
            $('.CustomDownPullSelect').find('select').val('');
            CompanyId=$('.CustomDownPullSelect').find('select').val();
            $('.MessageTable .Head .SEO button').on('click', function () {
                SEOval=$('.MessageTable .Head .SEO input').val();
                RenderVal(1,SEOval,CompanyId)
            });

            $(document).keyup(function (event) {
                switch (event.keyCode) {
                    case 13:
                        $(".MessageTable .Head .SEO button").click();
                        break;
                }
            });
        }
    });
    function RenderVal(page,SEOval,CompanyId) {
        var shopOptin = {
            shopName: SEOval||'',
            'limit': limit,
            "pager.offset": (page-1)*limit
        };
        if(!CompanyId==''){
            shopOptin['companyId']=CompanyId;
        }
        $.ajax({
            url: ctx+'/rentshop/selectRentshopByShopName',
            type: 'GET',
            data: shopOptin,
            success: function (data) {
                var OrderOption = {
                    data: {
                        Thead: {
                            PullDownAction: '操作',
                            Store: "门店名称",
                            SubordinateCompanies: "所属公司",
                            Site: '详细地址',
                            Linkman: '联系人',
                            Contact: '联系方式',
                            Remark: "备注"
                        },
                        Tbody: []
                    }
                };

                for (var i = 0; i < data.content.length; i++) {
                    (function () {
                        var index = i;
                        var OrderTbody = {
                            id: data.content[index].id,
                            PullDownAction: {
                                Default:{
                                    name: '编辑',
                                    action: function (e) {
                                        window.location.href=ctx+"/rentshop/updaterentshop?id="+data.content[index].id;
                                    }
                                },
                                data: [
                                    {
                                        name: '删除',
                                        action: function (e) {
                                            var id=e.parent().parent().parent().parent().data('id');

                                            var DeleteRentShop={
                                                data:{
                                                    Title:'删除提示',
                                                    Content:'是否要删除该门店',
                                                    OkChange:function () {
                                                        $.ajax({
                                                            url:ctx+'/rentshop/deleteRentshop',
                                                            data:{id:id},
                                                            type:'POST',
                                                            success:function (data) {
                                                                if(data.message=='删除成功'){
                                                                    new $().Overlays({data:{text:'删除成功',state:'Ok'}});
                                                                    // CompanyId='';
                                                                    // SEOval='';
                                                                    RenderVal(1);
                                                                }else {
                                                                    new $().Overlays({data:{text:'删除失败',state:'No'}});
                                                                }
                                                            }
                                                        })
                                                    }
                                                }
                                            };

                                            new $('body').ModalBox(DeleteRentShop)
                                        }
                                    }
                                ],
                            },
                            Store: data.content[index].shopName,
                            SubordinateCompanies: data.content[index].companyName,
                            Site: data.content[index].address,
                            Linkman: data.content[index].contacts,
                            Contact: data.content[index].telephone,
                            Remark: data.content[index].memo
                        };
                        OrderOption.data.Tbody.push(OrderTbody);

                    })()
                }
                var PagingMdOption = {
                    data: {
                        rows: 20,
                        nowrows: page,
                        Pages: '',
                        total:data.totalElements,
                        change: function (index) {
                            RenderVal(index,SEOval,CompanyId);
                        }
                    }
                };
                $('.Top').find('.CustomOrder').remove();
                $('.Top').find('.PagingMdPagingMd').remove();
                $('.Top').Order(OrderOption);
                $('.Top').PagingMd(PagingMdOption);

            }
        })
    }

    RenderVal(1,SEOval,CompanyId);

    $('.DataExportButton').on('click',function () {
        window.location.href=ctx+"/rentshop/exportRentShop?companyId="+CompanyId+"&shopName="+SEOval;
    })


});